package com.yanghs.common.listener;

import com.yanghs.common.dao.RedisDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

/**
 * @author yanghs
 * @Description:
 * @date 2018/3/22 13:07
 */
public class LabServletContextListener extends ContextLoaderListener {
    private static final Logger log = LoggerFactory.getLogger(LabServletContextListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        log.debug("开始启动系统...");
        super.contextInitialized(event);

    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        log.debug("开始关闭系统...");
        ServletContext servletContext = event.getServletContext();
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        RedisDao redisDao = (RedisDao) ctx.getBean("redisDao");
        try {
            redisDao.deleteAll();
            log.debug("清理redis缓存成功");
        } catch (Exception e) {
            log.debug("清理redis缓存失败");
            e.printStackTrace();
        }
        super.contextDestroyed(event);
    }
}
